package com.qingzhuge.manager.controller;

import com.qingzhuge.common.annotation.ASysLog;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.manager.controller.base.BaseUidPkLongController;
import com.qingzhuge.manager.dto.request.SysDeptRequestDto;
import com.qingzhuge.manager.dto.response.SysDeptResponseDto;
import com.qingzhuge.manager.entity.SysDept;
import com.qingzhuge.manager.service.ISysDeptService;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.qingzhuge.common.Constants.Controller.PATH_INFO;
import static com.qingzhuge.common.Constants.Controller.PATH_LIST;


/**
 * @author : zero.xiao
 * @description : 部门管理
 * @date :2018/6/8 0008 下午 14:49
 * @modified :
 */
@ASysLog(value = "部门")
@Api(tags = "S部门管理")
@AResource(name = "部门管理", type = EResourceType.menu, catalog = "系统管理")
@RestController
@RequestMapping("api/sys/auth/dept")
public class SysDeptController extends BaseUidPkLongController<SysDept, ISysDeptService, SysDeptRequestDto, SysDeptResponseDto> {

    @Override
    public ResponseEntity<ResponseBodyDto<PageUtil<SysDeptResponseDto>>> queryPage(@RequestBody(required = false) SysDeptRequestDto param) {
        return super.queryPage(param);
    }

    /**
     * 选择部门(添加、修改菜单)
     */
    @Override
    @GetMapping(PATH_LIST)
    public ResponseEntity<ResponseBodyDto<List<SysDeptResponseDto>>> queryList(@RequestBody(required = false) SysDeptRequestDto param) {
        return super.queryList(param);
    }

    /**
     * 上级部门Id(管理员则为0)
     */
    @ApiOperation(value = "上级部门Id")
    @GetMapping(PATH_INFO)
    public ResponseEntity<ResponseBodyDto<SysDeptResponseDto>> info() {
        SysDept deptId = null;
        if (null == getCurrUser().getRoot() || !getCurrUser().getRoot()) {
            List<SysDept> deptList = service.findAll();
            SysDept parent = null;
            for (SysDept sysDept : deptList) {
                if (parent == null) {
                    parent = sysDept.getParent();
                    continue;
                }

                if (parent.getId() > sysDept.getParent().getId()) {
                    parent = sysDept.getParent();
                }
            }
            deptId = parent;
        }

        return returnSuccess(deptId, SysDeptResponseDto.class);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysDeptResponseDto>> get(@PathVariable Long id) {
        return super.get(id);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysDeptResponseDto>> save(@RequestBody SysDeptRequestDto dept) {
        return super.update(dept);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysDeptResponseDto>> update(@RequestBody SysDeptRequestDto dept) {
        return super.update(dept);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable Long id) {
        return super.delete(id);
    }
}
